package top.minuy.util.helper;

import top.minuy.structure.linear.array.Array;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Scanner;
/**
 * @author Minuy
 * @time 11:46
 * @date 2021/9/11
 */
public class FileHelper {
    // 返回成功/失败，array存的所有单词
    public static boolean readFile(String fileName, Array<String> array){
        if(fileName == null || array == null){
            System.out.println("ReadFile failed, FileName or Array is null!");
            return false;
        }

        // 文件读取
        Scanner scanner;
        try {
            File file = new File(fileName);
            if (file.exists()){
                FileInputStream fileInputStream = new FileInputStream(file);
                scanner = new Scanner(new BufferedInputStream(fileInputStream), StandardCharsets.UTF_8);
                scanner.useLocale(Locale.ENGLISH);
            }else {
                System.out.println("ReadFile failed. File is not exists.");
                return false;
            }
        }catch (IOException ioe){
            System.out.println("ReadFile failed. Cannot open "+fileName+".");
            return false;
        }

        // 简单分词
        if(scanner.hasNextLine()){
            String contents = scanner.useDelimiter("\\A").next();
            int start = firstCharacterIndex(contents, 0);
            for (int i=start+1;i<=contents.length();) {
                if(i==contents.length()||!Character.isLetter(contents.charAt(i))){
                    String word = contents.substring(start,i).toLowerCase();
                    array.addLast(word);
                    start = firstCharacterIndex(contents, i);
                    i = start+1;
                }else{
                    i++;
                }
            }
        }
        return true;
    }

    // 返回所有内容
    public static String readFile(String fileName){
        if(fileName == null){
            System.out.println("ReadFile failed, FileName is null!");
            return null;
        }

        // 文件读取
        Scanner scanner;
        try {
            File file = new File(fileName);
            if (file.exists()){
                FileInputStream fileInputStream = new FileInputStream(file);
                scanner = new Scanner(new BufferedInputStream(fileInputStream), StandardCharsets.UTF_8);
                scanner.useLocale(Locale.ENGLISH);
            }else {
                System.out.println("ReadFile failed. File is not exists.");
                return null;
            }
        }catch (IOException ioe){
            System.out.println("ReadFile failed. Cannot open "+fileName+".");
            return null;
        }

        if(scanner.hasNextLine()){
            return scanner.useDelimiter("\\A").next();
        }
        return null;
    }

    private static int firstCharacterIndex(String s,int start){
        for (int i=start;i<s.length();i++){
            if(Character.isLetter(s.charAt(i))){
                return i;
            }
        }
        return s.length();
    }
}
